package se19team1e.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import se19team1e.gui.BaseScreen;
import se19team1e.gui.MessageBox;
import se19team1e.gui.MessageBoxFactory;

/**
 * 
 * An abstract base controller, to be inherited by all application controllers. 
 * 
 * <p>
 * Common, reusable functionalities can be placed in this class.
 * Currently, its main common function is to display a message box.
 * Other basic utility are a getter, a reference to itself and a constructor.
 * </p> 
 * 
 * @author lai shiaw san
 *
 */
public abstract class BaseController {

    /**
     * A reference to the current application instance.
     */
    private final Application application;
    
    /**
     * Get the reference to the current application instance.
     * @return Reference to the current application instance.
     */
    protected Application getApplication() {
        return this.application;
    }
    
    /**
     * Constructs a base controller, initializing with the application instance.
     * @param application Reference to the current application instance.
     */
    protected BaseController( Application application ) {
        this.application = application;
    }

    /**
     * Helper method to display a message box.
     * 
     * @param title Title to display.
     * @param message Message to display.
     * @param actionListener Callback when ok button is triggered in message box.
     */
    protected void displayMessage( String title, String message, final ActionListener actionListener ) {
        MessageBoxFactory messageBoxFactory = this.getApplication().getMessageBoxFactory();
        
        BaseScreen currentScreen = this.getApplication().getCurrentScreen();
        final MessageBox messageBox = messageBoxFactory.createMessageBox( currentScreen );
        
        messageBox.setTitle( title );
        messageBox.setMessage( message );
        messageBox.setActionListener( new ActionListener() {
            @Override
            public void actionPerformed( ActionEvent event ) {
               messageBox.hide();
               messageBox.dispose();
               
               actionListener.actionPerformed( event );
            }
        });
        
        messageBox.show();
    }

}
